﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlTypes;
using Logic;
using Helper;
using DBAccess;
using WHUT;


public partial class tokenTeam_message : System.Web.UI.Page
{
    IPagedDataProvider pdp = PagedDataProviderFactory.Instance();

    protected void Page_Load(object sender, EventArgs e)
    {
        pdp.ConnectionName = Common.GetDefaultSqlConnectionName();
        pdp.TableOrView = "t_messageboard";
        pdp.FieldsToReturn = "id,title, nickname, time_add, email, content";
        //pdp.FieldsToReturn = "*";
        pdp.FieldToOrder = "id";
        pdp.Ascending = false;
        pdp.PageSize = spPager.PageSize;
        pdp.RecordsFilter = "1=1";

        if (!IsPostBack)
        {
            BindMessage(true);
        }
    }

    private void BindMessage(bool bResetPager)
    {
        if (bResetPager)
        {
            spPager.CurrentPageIndex = 1;
            spPager.RecordsCount = pdp.TotalRecords;
        }

        pdp.CurrentPageIndex = spPager.CurrentPageIndex;
        rptMsgBox.DataSource = pdp.GetCurrentPageAsDataTable().DefaultView;
        rptMsgBox.DataBind();
    }

    /// <summary>
    /// 翻页控制
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SimplePager1_PageIndexChanging(object sender, wbh.PageChangingEvent e)
    {
        spPager.CurrentPageIndex = e.NewIndex;
        BindMessage(false);
    }


    protected void repMsgBox_ItemDataBound(object source, RepeaterItemEventArgs e)
    {
        string selStr = string.Empty;
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rep = e.Item.FindControl("rpt_MsgRelpy") as Repeater;//找到里层的repeater对象
            DataRowView rowv = (DataRowView)e.Item.DataItem; //找到分类Repeater关联的数据项 
            String deptId = Convert.ToString(rowv["Id"]);


            selStr = "SELECT [content],[reply_time] FROM [t_message_reply] WHERE [msg_id]=" + deptId;
            DataTable dt = Common.SQLQueryAsDataTable(Common.GetDefaultDBConnection(), selStr);
            rep.DataSource = dt;
            rep.DataBind();
        }
    }

    protected void msgDelett_Click(object sender, EventArgs e)
    {
        //int id = Convert.ToInt32(e.CommandName);
        int id = Function.IntSafeConvert(((ImageButton)sender).CommandArgument);
        string delStr = "DELETE FROM  [t_messageboard] WHERE [id]=" + id;
        try
        {
            Common.SQLUpdate(Common.GetDefaultDBConnection(), delStr, true, new Object[]{});
        }
        catch (Exception ex) 
        {
            Response.Redirect("~/error.htm");
            return;
        }
        BindMessage(true);
    }

    protected void msgReply_Click(object sender, EventArgs e)
    {
    }


    protected void replyDelet_Click(object sender, EventArgs e)
    {
    }

    protected void msgDelettAll_Click(object sender, EventArgs e)
    {
        bool flag = false;
        string delStr = "DELETE FROM  [t_messageboard] WHERE [id]=";
        for (int i = 0; i < rptMsgBox.Items.Count; i++)
        {
            //CheckBox ch = (CheckBox)rptMessage.Items[i].FindControl("ckbDelete");
            Label lb = (Label)rptMsgBox.Items[i].FindControl("lbMsgId");

            int id = Function.IntSafeConvert(lb.Text);
            if (!flag)
            {
                delStr += id;
                flag = true;
            }
            else
            {
                delStr += " or [id]=" + id;
            }
           
        }
        try
        {
            Common.SQLUpdate(Common.GetDefaultDBConnection(), delStr, true, new Object[] { });
        }
        catch (Exception ex)
        {
            Response.Redirect("~/error.htm");
            return;
        }
        BindMessage(true);
    }

    protected void msgDelettSelected_Click(object sender, EventArgs e)
    {

    }
}
